#include "waypointwidget.h"

WayPointWidget::WayPointWidget(unsigned long aID, QWidget* parent, Qt::WindowFlags fl):
    QWidget(parent, fl), ID(aID), state(NotRoute), item(NULL), x(0), y(0)/*myVisibility(false), activeRoute(false)*/
{
    setVisible(false);
    setMinimumSize(cPOINTSIZE, cPOINTSIZE);
    setMaximumSize(cPOINTSIZE, cPOINTSIZE);
    setGeometry(x, y, cPOINTSIZE, cPOINTSIZE);
}

WayPointWidget::~WayPointWidget()
{}

void WayPointWidget::mouseDoubleClickEvent(QMouseEvent* /*event*/)
{
    emit Edited(ID);
}

void WayPointWidget::setOwnVisibility(const bool& aVisible)
{
    setVisible(aVisible);
    if(item)    item->setVisible(aVisible);
}

void WayPointWidget::setItem(QGraphicsProxyWidget *aItem)
{
    if(!aItem)   return;
    item = aItem;
    item->setZValue(cPOINTZVALUE);
    item->setVisible(isVisible());
}

void WayPointWidget::setPosition(const int &aX, const int &aY)
{
    x = aX;
    y = aY;
    if(item)
    {
        item->setX(x);
        item->setY(y);
    }
}

int WayPointWidget::getX(void) const
{
    return x;
}

int WayPointWidget::getY(void) const
{
    return y;
}

void WayPointWidget::paintEvent(QPaintEvent* /*event*/)
{
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing, true);
    int side = qMin(width(), height());
    painter.setViewport((width() - side) / 2, (height() - side) / 2,
                        side, side);
    painter.setWindow(-50, -50, 100, 100);
    draw(&painter);
}

void WayPointWidget::draw(QPainter *painter)
{
    QPen thickPen(palette().foreground(), 1.5);
    QPen thinPen(palette().foreground(), 0.5);
    QColor pointColor;
    switch(state)
    {
    case NotRoute:
        pointColor = Qt::darkYellow;
        break;
    case FirstPoint:
        pointColor = Qt::darkRed;
        break;
    case MiddlePoint:
        pointColor = Qt::darkBlue;
        break;
    case EndPoint:
        pointColor = Qt::darkGreen;
        break;
    default:
        pointColor = Qt::darkYellow;
    };
    painter->setPen(thinPen);
    painter->setBrush(palette().foreground());

    QConicalGradient coneGradient(0, 0, -90.0);
    coneGradient.setColorAt(0.0, pointColor);
    coneGradient.setColorAt(0.5, Qt::white);
    coneGradient.setColorAt(1.0, pointColor);

    painter->setBrush(coneGradient);
    painter->drawEllipse(-46, -46, 92, 92);

    QRadialGradient haloGradient(0, 0, 20, 0, 0);
    haloGradient.setColorAt(0.0, pointColor);
    haloGradient.setColorAt(0.8, pointColor);
    haloGradient.setColorAt(0.9, Qt::white);
    haloGradient.setColorAt(1.0, Qt::black);

    painter->setPen(Qt::NoPen);
    painter->setBrush(haloGradient);
    painter->drawEllipse(-20, -20, 40, 40);

    QLinearGradient knobGradient(-7, -25, 7, -25);
    knobGradient.setColorAt(0.0, Qt::black);
    knobGradient.setColorAt(0.3, pointColor);
    knobGradient.setColorAt(0.8, Qt::white);
    knobGradient.setColorAt(1.0, Qt::black);
}
